<template>
  <div class="departments-container">
    <el-card class="tree-card">
      <tree-tools
        :tree-node="treeCompany"
        :is-show="false"
        @addDepts="addDepts"
        >
      </tree-tools>

      <el-tree
        :data="departs"
        :props="defaultProps"
        default-expand-all>
        <tree-tools
          slot-scope="{ data }"
          @addDepts="addDepts"
          @delDepts="getDepartments"
          @editDepts="editDepts"
          :tree-node="data">
        </tree-tools>
      </el-tree>
    </el-card>

    <add-dept
      :isShowAdd.sync="isShowAdd"
      :node="node"
      @addDepts="getDepartments"
      ref="addDept"
    />
  </div>
</template>

<script>
import TreeTools from './components/tree-tools.vue'
import AddDept from './components/add-dept.vue'
import { getDepartments } from '@/api/department'
import { filterDepts } from '@/utils/index'

export default {
  name: 'department-box',
  components: {
    TreeTools,
    AddDept
  },
  data(){
    return {
      treeCompany: {
        name: '江苏传智播客教育科技股份有限公司',
        manager: '负责人'
      },
      departs: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      isShowAdd: false,
      node: {}
    }
  },
  created () {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      const result = await getDepartments()

      this.treeCompany = { name: result.companyName, manager: '负责人', id: '' }

      const departs = filterDepts(result.depts, '')

      this.departs = departs
    },
    addDepts(node) {
      this.node = node
      this.isShowAdd = true
    },
    editDepts(node) {
      this.node = node
      this.isShowAdd = true
      this.$refs.addDept.getDepartDetail(node.id)
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
